package com.kehao.leetcode.jzoffer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class JZSolution061 {
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        List<List<Integer>> result = new ArrayList<>();
        PriorityQueue<List<Integer>> queue = new PriorityQueue<>((arr1,arr2)->arr1.get(0)+arr1.get(1)-arr2.get(0)-arr2.get(1));
        for (int i = 0; i<k && i < nums1.length; i++) {
            for (int j = 0; j < k && j < nums2.length; j++) {
                queue.add(Arrays.asList(nums1[i],nums2[j]));
            }
        }
        for (int i = 0; i < k; i++) {
            List<Integer> poll = queue.poll();
            if(poll!=null){
                result.add(poll);
            }
        }
        return result;
    }
}
